import { POST, GET, api } from '@/utils/request';
import { message } from "antd"
export default {
    namespace: 'shares',
    state: {

        dataSource: [],
        
        shrinkData: {},
        halfYearDay: {},
        followData: [],
        shrinkDay: {}
    },
    effects: {
        // 查询最近交易日的分钟k线
        *queryMinuteKlines({ payload }, { call, put }) {
            const { rc, data } = yield call(GET, api.shares.minute, payload, true, false);
            if (rc === 0) {
                yield put({
                    type: "save",
                    payload: {
                        dataSource: data
                    }
                })
            }
        },
        // 查询日k线
        *queryDayKlines({ payload }, { call, put, select }) {
          const _payload = {
              ...payload,
              fields1: 'f1,f2,f3,f4,f5,f6',
              klt: 101,
              fqt: 0,
              end: '20500101',
          }
          const { rc, data } = yield call(GET, api.shares.day, _payload);
          const { shrinkDay } = yield select(({ shares }) => shares)
          if (rc === 0) {
              yield put({
                  type: "save",
                  payload: {
                    shrinkDay: {
                          ...shrinkDay,
                          [data.code] : data
                      }
                  }
              })
          }
      },
        // 查询半年的日k线
        *queryHalfYearDayKlines({ payload }, { call, put, select }) {
            const _payload = {
                ...payload,
                fields1: 'f1,f2,f3,f4,f5,f6',
                klt: 101,
                fqt: 0,
                end: '20500101',
                lmt: 120
            }
            const { rc, data } = yield call(GET, api.shares.halfYearDay, _payload);
            const { halfYearDay } = yield select(({ shares }) => shares)
            if (rc === 0) {
                yield put({
                    type: "save",
                    payload: {
                        halfYearDay: {
                            ...halfYearDay,
                            [data.code] : data
                        }
                    }
                })
            }
        },
        // 查询所有的日k线
        *queryFullDayKlines({ payload }, { call, put }) {
            const _payload = {
                ...payload,
                fields1: 'f1,f2,f3,f4,f5,f6',
                fields2: 'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61',
                klt: 101,
                fqt: 0,
                end: '20500101',
                lmt: 120
            }
            const { rc, data } = yield call(GET, api.shares.fullDay, _payload);
            if (rc === 0) {
                yield put({
                    type: "save",
                    payload: {
                        dataSource: data
                    }
                })
            }
        },
        // 查询周k线
        *queryWeekKlines({ payload }, { call, put }) {
            const { rc, data } = yield call(GET, api.shares.week, payload);
            if (rc === 0) {
                yield put({
                    type: "save",
                    payload: {
                        dataSource: data
                    }
                })
            }
        },
        // 查询周k线
        *queryTwoWeeksKlines({ payload }, { call, put }) {
            const { rc, data } = yield call(GET, api.shares.twoWeeks, payload);
            if (rc === 0) {
                yield put({
                    type: "save",
                    payload: {
                        dataSource: data
                    }
                })
            }
        },
        // 查询订阅股票
        *queryFollow({ payload, callBack }, { call, put }) {
            const { code, result } = yield call(GET, api.shares.queryFollow, payload);
            if (code === 0) {
                yield put({
                    type: "save",
                    payload: {
                        followData: result
                    }
                })
                callBack && callBack()
            }
        },
        // 新增订阅股票
        *addFollow({ payload }, { call, put }) {
            const { code } = yield call(POST, api.shares.addFollow, payload);
            if (code === 0) {
                message.success("订阅成功")
                // yield put({
                //     type: "save",
                //     payload: {
                //         addFileVisible: false,
                //         modalVisible: false
                //     }
                // })
                yield put({
                    type: "queryFollow"
                })
            }
        },
        // 删除订阅股票
        *deleteFollow({ payload }, { call, put }) {
            const { code } = yield call(POST, api.shares.deleteFollow, payload);
            if (code === 0) {
                message.success("取消订阅成功")
                yield put({
                    type: "queryFollow"
                })
            }
        },
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state, ...payload
            };
        }
    },
};
